package chain_user_config

import (
	"chain_bridge_service/internal/pkg/core"
	chainConfig "chain_bridge_service/internal/repository/mongodb/chain_config"
	chainUserConfig "chain_bridge_service/internal/repository/mongodb/chain_user_config"
)

type SearchData struct {
	UserId string // 用户id
}

type ChainUserConfigData struct {
	Id              string `json:"id"`
	CollectAddress  string `json:"collectAddress"`
	FeeAddress      string `json:"feeAddress"`
	UserId          string `json:"userId"`
	ChainConfigId   string `json:"chainConfigId"`
	ChainConfigName string `json:"chainConfigName"`
}

func (s *service) GetConfigByUserId(ctx core.Context, userId string, pageIndex uint, pageSize uint) (int64, []ChainUserConfigData, error) {

	model := chainUserConfig.NewModel()
	model.UserId = userId
	total, err := model.Count(s.db.GetDb())
	list, err := model.GetConfigByUserId(s.db.GetDb(), pageIndex, pageSize)
	var result []ChainUserConfigData

	for _, value := range list {
		var config ChainUserConfigData
		config.CollectAddress = value.CollectAddress
		config.FeeAddress = value.FeeAddress
		config.UserId = value.UserId
		config.ChainConfigId = value.ChainConfigId
		config.Id = value.Id
		chainModel := chainConfig.NewModel()
		chainConfigInfo, err := chainModel.GetById(s.db.GetDb(), value.ChainConfigId)
		if err != nil {
			return 0, nil, err
		}

		config.ChainConfigName = chainConfigInfo.ChainName
		result = append(result, config)
	}

	return total, result, err
}
